#!/usr/bin/env python 
# -*- coding: utf-8 -*-

from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
import time

_from_addr = "guanfuchang@4399.com"  # 自定义发送邮箱
_password = "Loveyp1314"  # 发送邮箱登录密码
_to_addr = [_from_addr]  # 自定义收件人列表
_smtp_server = "smtp.exmail.qq.com"  # SMTP服务器，企业QQ邮箱


def send_mail(to_list=_to_addr, sub="邮件功能调试默认主题", content="邮件功能调试默认邮件内容"):
    """ to_list：收件人列表；sub：主题；content：邮件内容 """
    msg = MIMEText(content, _subtype='html',
                   _charset='utf-8')  # 创建一个实例，这里设置为html格式邮件
    msg['From'] = '4399PTQA'
    msg['To'] = ";".join(to_list)
    msg['Subject'] = Header(sub, 'utf-8').encode()  # 设置主题
    try:
        server = smtplib.SMTP(_smtp_server, 25)
        # server.set_debuglevel(1)
        server.login(_from_addr, _password)
        server.sendmail(_from_addr, to_list, msg.as_string())
        server.quit()
        return True
    except Exception as e:
        print(str(e))
        return False


if __name__ == '__main__':
    title = "SDK测试用例:" + time.strftime("%Y-%m-%d %H:%M:%S",
                                       time.localtime())  # 邮件主题
    msg = """
    Hello Milton
"""
    to_addr = ["guan825@126.com"]
    if send_mail(to_list=to_addr, sub=title, content=msg):
        print("发送成功")
    else:
        print("发送失败")
